<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/2/12
 * Time: 1:19 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

/**
 * Phongtructhuoc Model
 */
class VastModelPhongtructhuoc extends JModelForm
{
    public function createItem($data) 
    {
        // set the data into a query to update the record
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->clear();
        $query->insert('#__phong');
        
        foreach($data as $key => $value) {
            if (is_numeric($value)) {
                $query->set($key . ' = ' . $value);
            } else {
                $query->set($key . ' = ' . $db->quote($value));
            }
        }
                
        $db->setQuery($query);
        
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            JVastLog::addLog(date('Y-m-d'), $_SERVER['REMOTE_ADDR'], 'Thêm phòng mới ' . $data['ten'], JFactory::getUser()->id, VastHelper::getHosocanbo()->id);
            return true;
        }
    }
        
    public function getForm($data = array(), $loadData = true)
    {
        $app = JFactory::getApplication('site');        
        // Get the form.        
        $form = $this->loadForm('com_vast.phongtructhuoc', 'phongtructhuoc', array('control' => 'jform', 'load_data' => true));
                
        if (empty($form)) {
            return false;
        }
        
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id'))
        {
            $id =  $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else
        {
            $id =  $jinput->get('id', 0);
        }
                        
        $user = JFactory::getUser();
                
        return $form;
    }

    /**
     * Method to get the data that should be injected in the form.
     *
     * @return	mixed	The data for the form.
     * @since	1.6
     */
    protected function loadFormData()
    {             
        $data = array();              
        $data['donvi_id'] = JRequest::getInt('id', 0);

        return $data;
    }

    public function getKhoi() 
    {
        $id = JRequest::getInt('id');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*');
        $query->from('#__donvi as a');
        $query->where('a.id = ' . $id);
            
        $db->setQuery($query);

        $donvi = $db->loadObject(); 
        if ($donvi !== NULL)
        {
            $khoiId = $donvi->khoi_id;
            $query = $db->getQuery(true);
                
            $query->select('a.*');
            $query->from('#__khoi as a');
            $query->where('a.id = ' . $khoiId);
                
            $db->setQuery($query);
    
            return $db->loadObject();                
        }               
        
        return NULL;
    }
        
    public function getDonvi() {
        $id = JRequest::getInt('id');
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
                
        $query->select('a.*');
        $query->from('#__donvi as a');
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        $donvi = $db->loadObject();
        
        return $donvi;
    }
    
}